package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.po.MarketGoodsExample;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.po.MarketTopicExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.topic.vo.AdminTopicReadVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.service.adminservice.TopicService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 楚夏三分
 * @datetime 2022/12/30 9:12
 * @description: TODO:
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public CommonData<MarketTopic> list(BasePageInfo info, String title, String subtitle) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketTopicExample example = new MarketTopicExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketTopicExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtil.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketTopic> list = topicMapper.selectByExampleWithBLOBs(example);
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketTopic create(MarketTopic marketTopic) {
        marketTopic.setAddTime(new Date());
        topicMapper.insertSelective(marketTopic);
        return marketTopic;
    }

    @Override
    public AdminTopicReadVo read(Integer id) {
        // 创建VO对象
        AdminTopicReadVo adminTopicReadVo = new AdminTopicReadVo();
        // 赋值成员变量
        MarketTopic topic = topicMapper.selectByPrimaryKey(id);
        adminTopicReadVo.setTopic(topic);
        List<Integer> goodsId = Arrays.asList(topic.getGoods());
        if (goodsId.size() != 0){
            MarketGoodsExample example = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(goodsId);
            List<MarketGoods> marketGoods = goodsMapper.selectByExample(example);
            adminTopicReadVo.setGoodsList(marketGoods);
        }
        return adminTopicReadVo;
    }

    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        topicMapper.updateByPrimaryKeySelective(marketTopic);
        return null;
    }

    @Override
    public void delete(MarketTopic marketTopic) {
        marketTopic.setDeleted(true);
        topicMapper.updateByPrimaryKeySelective(marketTopic);
    }

    @Override
    public void batchDelete(List<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            topicMapper.updateDeletedByIdLogically(ids.get(i));
        }
    }
}
